﻿using MoetutuServer.Helper;
using MoetutuServer.Pixiv.ProcessImg;
using MoetutuServer.WebSite.Pixiv;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MoetutuServer.PublishHelper
{
    class UploadImg
    {
        /// <summary>
        /// 两次上传图床，获取链接。
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <returns></returns>
        public static List<string> GetPicBed(String Imgurl, string Site)
        {
            List<String> ImgList = new List<string>();
            ImgList.Add(Imgurl);
            String Filename = GetImg(Imgurl, Site);
        //    Helper.ImageHelper.CompressImage(Filename, Filename);//进行一次压缩
            if (Filename != "")
            {

                string SinaLink = UploadToTourou(Filename);
                Console.WriteLine(SinaLink);

                if (SinaLink != "")
                    ImgList.Add(SinaLink);

                /*
                String PixHost = Pixhost.UploadImgAsync(Filename);
                if (PixHost != "")
                    ImgList.Add(PixHost);*/
            }
            Console.WriteLine("共有{0}张图", ImgList.Count);
            return ImgList;
        }

        private static string GetImg(string Imgurl, string site)
        {
            String FileName = "";
            if (site == "Pixiv")
                FileName = PixivHelper.PixivDownload(Imgurl);
            if (site == "Konachan")
                FileName =Helper.WebHelper.FileDownload(Imgurl);
            return FileName;




        }


        /// <summary>
        /// 上传图片到偷揉
        /// </summary>
        /// <param name="Filename"></param>
        /// <returns></returns>
        public static string UploadToTourou(String Filename)
        {
            String SinaUrl = "";

            using (WebClient UploadWebClient = new WebClient())
            {
                UploadWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");

                try
                {
                    Console.WriteLine("上传中");
                    SinaUrl = UpLoadToNewApi(Filename, UploadWebClient);
                    //   SinaUrl = UpLoadToOldApi(Filename, UploadWebClient);
                }
                catch (Exception ex)
                {

                    PrintLog.Log(ex);
                }

            }
            return SinaUrl;
        }
      
        /// <summary>
        /// 传给新的api
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="UploadWebClient"></param>
        /// <returns></returns>
        private static string UpLoadToNewApi(string Filename, WebClient UploadWebClient)
        {
            string SinaUrl = "";

            String API = GetApi();
            if (API != "")

            {
                try
                {
                    byte[] responseArray = UploadWebClient.UploadFile(API, Filename);
                    String JsonText = Encoding.UTF8.GetString(responseArray);
                    Console.WriteLine(JsonText);
                    NewRoot AllJson = JsonConvert.DeserializeObject<NewRoot>(JsonText);
                    if (AllJson.wbpid != null)
                        SinaUrl = "https://wx1.sinaimg.cn/large/" + AllJson.wbpid + ".jpg";

                    else
                        SinaUrl = Danran.UploadFile(Filename);
                }  catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    SinaUrl = Danran.UploadFile(Filename);
                }
               
            }
            return SinaUrl;
        }

        public static string GetApi()
        {
            String API = "";
            string GetJson = Helper.WebHelper.HttpGet("http://danmu.fm/api/hosts");
            try
            {
                GetHost AllJson = JsonConvert.DeserializeObject<GetHost>(GetJson);
                API = AllJson.cmcc;
                API = "http://" + new Uri(API).Host + ":672/v1/upload";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return API;
        }
    }
    public class GetHost
    {
        /// <summary>
        /// 
        /// </summary>
        public string cmcc { get; set; }
    }


    public class OldRoot
    {
        /// <summary>
        /// a15b4afegy1fnpl5w0prhj20m80xcgq2
        /// </summary>
        public string pid { get; set; }
        /// <summary>
        /// jpeg
        /// </summary>
        public string type { get; set; }
    }
    public class NewRoot
    {
        /// <summary>
        /// 005zWjpnly1fnwgth7xj7j30av06474j
        /// </summary>
        public string wbpid { get; set; }
        /// <summary>
        /// d41d8cd98f00b204e9800998ecf8427e
        /// </summary>
        public string md5 { get; set; }
        /// <summary>
        /// jpg
        /// </summary>
        public string type { get; set; }
        /// <summary>
        /// /home/git/sinaimg.cx/img/4j/005zWjpnly1fnwgth7xj7j30av06474j.jpg
        /// </summary>
        public string path { get; set; }
        /// <summary>
        /// c195a871aebc090613ecaad35935ca7af25c57644cd8-deuIQc.jpg
        /// </summary>
        public string original { get; set; }
    }
}
